package com.inspur.edp.common.expr.expresses.compare;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.inspur.edp.common.expr.ExprJsonDeserializer;
import com.inspur.edp.common.expr.Express;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/12/12 10:18
 */

@Data
public class CompareExpr implements Express {

    public static final String KIND = "compare";

    @JsonDeserialize(using = ExprJsonDeserializer.class)
    private Express leftExpress;

    @JsonDeserialize(using = CompareOperatorDeserializer.class)
    private CompareOperator operator;

    @JsonDeserialize(using = ExprJsonDeserializer.class)
    private Express rightExpress;

    @Override
    public String getKind() {
        return KIND;
    }

    public static class CompareOperatorDeserializer extends JsonDeserializer<CompareOperator> {

        @Override
        public CompareOperator deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            JsonNode node = jsonParser.getCodec().readTree(jsonParser);
            if (node == null || StringUtils.isEmpty(node.asText())) {
                return null;
            }
            return CompareOperator.valueOf(node.asText());
        }
    }
}
